@extends('layouts.app')

@section('content')
<div class="content content_business">
	<div class="button">
		<button class="add_business" data-type="business">添加商家</button>
		<div class="right"><span class="open">展开</span> | <span class="close active">收起</span></div>
	</div>
	<table class="title">
		<thead>
			<tr>
				<th>商家名称</th><th>活动名称</th><th>订单渠道</th><th>创建时间</th><th>操作</th>
			</tr>
		</thead>
	</table>
	<div class="table_list" id="list">
	</div>
</div>
@endsection

@section('scripts')
<script type="text/javascript">
	params['page'] = 1;
	params['pageSize'] = 10;
    getList('business', params);
	$(function(){
		$('.page_content').on('click','.add_business',function(){
			var type = $(this).attr('data-type');
			$.ajax({
		        type: "GET",
		        url: "/business/create/"+type,
		        dataType: "html",
		        success: function(res) {
					art.dialog({
						title: '添加商家',
						width: '497px',
						lock: true,
						id: 'add',
						content: res,
					});
				}
			});
		})
		$('#list').on('click','#edit',function(){
			var type = $(this).attr('data-type');
			var id = $(this).attr('data-id');
			$.ajax({
		        type: "GET",
		        url: "/business/edit/"+id+'/'+type,
		        dataType: "html",
		        success: function(res) {
					art.dialog({
						title: '添加商品',
						width: '497px',
						lock: true,
						id: 'edit',
						content: res,
					});
				}
			});
		})

		$('#list').on('click','#add',function(){
			var type = $(this).attr('data-type');
			var url;
			var title;
			if(type != 'business'){
				var id = $(this).attr('data-id');
				url ="/business/create/"+type+'?id='+id;
				if (type=='activity') {
					title='添加活动';
				}else{
					title='添加渠道';
				}
			}else{
				url ="/business/create/"+type
			}
			$.ajax({
		        type: "GET",
		        url: url,
		        dataType: "html",
		        success: function(res) {
					art.dialog({
						title: title,
						width: '497px',
						lock: true,
						id: 'edit',
						content: res,
					});
				}
			});
		})

		$('#list').on('click','#del',function(){
			var id = $(this).prev().attr('data-id');
			$.ajax({
		        type: "delete",
		        url: '/business/'+id,
		        dataType: "html",
		        success: function(res) {
					art.dialog({
						title: '提示',
						width: '497px',
						lock: true,
						id: 'edit',
						content: res,
				});
				getList('business', params);
				}				
			});
		});

		$('body').on('click','.true',function(){		
			var form = new Object();
			form.activity_id = $('#activity').attr('data-id');
			form.type_id = $('#type').attr('data-id');
			form.name = $('#business').val();
			form.id = $(this).attr('data-id');
			form.type = $(this).attr('data-type');
			form.action = $(this).attr('data-action');
			if(form.name == '') {
				box.tipHover({ status: 0, msg: "请输入商家名称" });
				// alert('请输入商家名称');
				return false;
			}else{

			}
			if(form.activity_id == undefined || form.activity_id == '') {
				box.tipHover({ status: 0, msg: "请选择活动" });
				// alert('请选择活动');
				return false;
			}
			if(form.type_id == undefined || form.type_id == '') {
				box.tipHover({ status: 0, msg: "请选择渠道" });
				// alert('请选择渠道');
				return false;
			}

			$.ajax({
		        type: "POST",
		        url: '/business',
		        dataType: "json",
		        data: form,
		        success: function(res) {
		        	$('.cancel').click();
					if(res.code == 1) {
						// alert(res.msg);
						box.tipHover({ status: 0, msg: "商家已存在该渠道" });
						return false;
					}
    				getList('business', params);
				},
				error:function(res){
					box.tipHover({ status: 0, msg: "商家名称已存在！" });
					// alert('商家名称已存在！')
				}
			});
		})
	})
</script>
@endsection
